package gee

import (
	"log"
	"net/http"
	"strings"
)

type HandlerFunc func( *Context)

type RouterGroup struct {
	prefix string
	middlewares []HandlerFunc
	engine *Engine
}

type Engine struct {
	*RouterGroup
	router *router
	groups []*RouterGroup
}

func New() *Engine {
	engine := &Engine{router: newRouter()}
	engine.RouterGroup = &RouterGroup{engine: engine}
	engine.groups = []*RouterGroup{engine.RouterGroup}
	return engine
}

func (group *RouterGroup)Group(prefix string) *RouterGroup {
	engine := group.engine
	newGroup := &RouterGroup{
		prefix: group.prefix + prefix,
		engine: engine,
	}
	engine.groups = append(engine.groups,newGroup)
	return newGroup
}

func (group *RouterGroup) Use(middlewares ...HandlerFunc)  {
	group.middlewares = append(group.middlewares,middlewares...)
}

func (group *RouterGroup) addRoute(method,pattern string ,handler HandlerFunc)  {
	completely_pattern := group.prefix + pattern
	log.Printf("Route %4s - %s",method,completely_pattern)
	group.engine.router.addRoute(method,completely_pattern,handler)
}

func (group *RouterGroup) GET (pattern string,handler HandlerFunc)  {
	group.addRoute("GET",pattern,handler)
}

func (group *RouterGroup) POST (pattern string,handler HandlerFunc)  {
	group.addRoute("POST",pattern,handler)
}

func (engine *Engine) Run(addr string) (err error) {
	//fmt.Println("kkk")
	//fmt.Println(len(engine.groups))
	//for _,group := range engine.groups {
	//	fmt.Println(group.prefix)
	//}
	return http.ListenAndServe(addr,engine)
}

func (engine *Engine)ServeHTTP(w http.ResponseWriter,req *http.Request)  {
	var middlewares []HandlerFunc
	for _,group := range engine.groups {
		if strings.HasPrefix(req.URL.Path,group.prefix){
			middlewares = append(middlewares,group.middlewares...)
		}
	}
	c := newContext(w,req)
	c.handlers = middlewares
	engine.router.handle(c)
}